/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.adapter;

import java.util.ArrayList;
import java.util.List;

import android.view.View;

import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.model.Marker;

/**
 * <p>
 * Adapter for supporting multiple types of different {@link InfoWindowAdapter}s
 * on one map.
 * </p>
 * <p>
 * This adapter delegates calls to {@link #getInfoContents(Marker)} and
 * {@link #getInfoWindow(Marker)} to child {@link CustomInfoWindowAdapter}s,
 * which support the provided markers.
 * </p>
 * <p>
 * Whether a {@link CustomInfoWindowAdapter} supports a particular marker is
 * determined by ({@link CustomInfoWindowAdapter#isSupported(Marker)})
 * </p>
 * <p>
 * If there are multiple child adapters which support the same {@link Marker},
 * the child with the lowest index will be used as that {@link Marker}'s info
 * window adapter
 * </p>
 * 
 * @author Justas
 */
public class MultiInfoWindowAdapter implements InfoWindowAdapter {
    
    private List<CustomInfoWindowAdapter> adapters = new ArrayList<CustomInfoWindowAdapter>();

    @Override
    public View getInfoContents(Marker marker) {
        for (CustomInfoWindowAdapter adapter : adapters) {
            if (adapter.isSupported(marker)) {
                return adapter.getInfoContents(marker);
            }
        }
        return null;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        for (CustomInfoWindowAdapter adapter : adapters) {
            if (adapter.isSupported(marker)) {
                return adapter.getInfoWindow(marker);
            }
        }
        return null;
    }

    /**
     * Adds a child adapter
     * 
     * @param adapter
     */
    public void registerAdapter(CustomInfoWindowAdapter adapter) {
        adapters.add(adapter);
    }
    
    /**
     * Info window adapter, which is tailored to a specific type of marker
     * 
     * @author Justas
     */
    public static interface CustomInfoWindowAdapter extends InfoWindowAdapter {
        
        /**
         * Indicates whether this info window can be displayed for the provided marker.
         * 
         * @param marker
         * @return
         */
        boolean isSupported(Marker marker);
    }

}
